package edu.gatech.cs2340.mule.model;
import java.awt.Point;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.Random;

/**
 * 
 * This class will keep track of the properties that are not currently
 * owned by any of the players. It will also calculate the buying or
 * selling price of land if any land transaction with the LandOffice
 * ever occurs.
 * 
 * @author Keegan Nesbitt
 *
 */
public class LandOffice implements Serializable {
	private static final long serialVersionUID = 6305654537350508911L;
	
	private static final int BASE_SELLING_PRICE = 400;
	private static final int BASE_BUYING_PRICE = 300;
	
	private LinkedList<Point> unownedLand;
	
	/**
	 * Initializes the LandOffice's unownedLand to an empty LinkedList
	 * 
	 */
	public LandOffice() {
		unownedLand = new LinkedList<Point>();
	} // end LandOffice constructor
	
	/**
	 * 
	 * @param gameMap The map for the game
	 */
	public void stockInitialProperties(Map gameMap) {
		for(int row = 0; row < gameMap.getMapHeight(); row++) {
			for(int column = 0; column < gameMap.getMapWidth(); column++) {
				MapTile currentTile = gameMap.getMapTileAt(new Point(row,column));
				if(!(currentTile instanceof Town)) {
					unownedLand.add(new Point(row, column));
				}
			} // end column for loop
		} // end row for loop
	} // end stockInitialProperties method
	
	/**
	 * 
	 * Calculates the selling price for property using the base price and
	 * a random number and returns the value
	 * 
	 * @param rand The Random object used to calculate the selling price
	 * @return The selling price for selling property to the land office
	 */
	public int calculateSellingPrice(Random rand) {
		return BASE_SELLING_PRICE + rand.nextInt(201);
	} // end calculateSellingPrice method
	
	/**
	 * 
	 * Calculates the buying price for property using the base price, round number,
	 * and a random number and returns the value. If the land is being purchased within
	 * the first two rounds of the game, the purchase is a free land grant.
	 * 
	 * @param rand The Random object used to calculate the buying price
	 * @param roundNumber The round number to be used to calculate the buying price
	 * @return The buying price for an unowned terrain tile
	 */
	public int calculateBuyingPrice(Random rand, int roundNumber) {
		return BASE_BUYING_PRICE + (roundNumber * rand.nextInt(101));
	} // end calculateBuyingPrice method
	
	/**
	 * 
	 * Determines if the location of the map is not owned by any of the players
	 * 
	 * @param location The location to check whether or not it is unowned
	 * @return True if the property is not owned by a player, false otherwise
	 */
	public boolean isUnowned(Point location) {
		for(Point pt : unownedLand) {
			if(pt.x == location.x && pt.y == location.y) {
				return true;
			}
		}
		
		return false;
	}
	
	/**
	 * 
	 * The location of the map tile provided will be removed from the LandOffice's
	 * inventory of unowned land.
	 * 
	 * @param location The location of the map tile to be removed from the land office
	 */
	public void removeUnownedLand(Point location) {
		unownedLand.remove(location);
	}
	
	/**
	 * 
	 * The location of the map tile provided will be added to the LandOffice's
	 * inventory of unowned land.
	 * 
	 * @param location The location of the map tile to be added to the land office
	 */
	public void addUnownedLand(Point location) {
		unownedLand.add(location);
	}
} // end LandOffice class
